using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml.Linq;
using WindowsPhonePostClient;
using System.Net.NetworkInformation;

namespace LyonMag
{
    public partial class Poll : PhoneApplicationPage
    {

        string pollid = string.Empty;
        string answerid = string.Empty;
        string polllink = "http://www.lyonmag.com/polls.php";
        public Poll()
        {
            InitializeComponent();
            GetPoll();
        }
        public void GetPoll()
        {
            try
            {
                WebClient wc = new WebClient();
                wc.DownloadStringAsync(new Uri("http://www.lyonmag.com/win8/poll_answer"));
                wc.DownloadStringCompleted += wc_DownloadStringCompleted;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
        public void postforum()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            //  action=add_comment&id=" + [NEWS_ID] + "&type=article&account_id=" + [ACCOUNT_ID] + "&name=" + [USERNAME] + "&text=" + [COMMENT_TEXT] 
            parameters.Add("action", "add_answer");
        
            parameters.Add("poll_id", pollid);
            parameters.Add("answer", answerid);
       
            PostClient proxy = new PostClient(parameters);
            proxy.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    
                    string data = e.Result;


                    MessageBox.Show("Merci de votre participation au sondage.", "Choix validé !", MessageBoxButton.OK);

                  //  MessageBox.Show(data);
                 

                }
            };
            proxy.DownloadStringAsync(new Uri(polllink, UriKind.Absolute));
        }
        void wc_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            try
            {
                string peopleXMLPath = e.Result.ToString();
                XDocument loadedData = XDocument.Parse(peopleXMLPath);

                var data = from query in loadedData.Elements("lyon_mag").Elements("poll")
                           select new PollClass.PollMain
                           {
                               Question = (string)query.Element("question"),
                              
                               Id = (string)query.Element("id"),


                               Poll_Responses = new List<PollClass.PollResponses>(from sz in query.Elements("reponses").Elements("reponse")
                                                                             select new PollClass.PollResponses()
                                                                             {
                                                                                Reponse_txt  = (string)sz.Element("reponse_txt"),
                                                                                 Reponse_type = (string)sz.Element("reponse_type"),
                                                                                 Reponse_id = (string)sz.Element("reponse_id"),
                                                                                Reponse_resultat = (string)sz.Element("reponse_resultat")

                                                                             }).ToList()




                           };

                foreach (PollClass.PollMain dat in data)
                {
                    txtPollQuestion.Text = dat.Question.ToString();
                    Opt1.Text = dat.Poll_Responses[0].Reponse_txt;
                    pollid = dat.Id;
                    Opt2.Text = dat.Poll_Responses[1].Reponse_txt;
                    Opt3.Text = dat.Poll_Responses[2].Reponse_txt;
                    Opt4.Text = dat.Poll_Responses[3].Reponse_txt;
                    Opt1.Tag = dat.Poll_Responses[0].Reponse_id;
                    Opt2.Tag = dat.Poll_Responses[1].Reponse_id;
                    Opt3.Tag = dat.Poll_Responses[2].Reponse_id;
                    Opt4.Tag = dat.Poll_Responses[3].Reponse_id;
                }
                prgs.Visibility = Visibility.Collapsed;
              
            }
            catch (Exception ex)
            {

                if (NetworkInterface.GetIsNetworkAvailable() == true)
                {
                    // MessageBox.Show(NetworkInterface.GetIsNetworkAvailable().ToString());
                }
                else
                {
                    MessageBox.Show("Internet Not Availabe", "LyonMag", MessageBoxButton.OK);
                }
                ex.Message.ToString();
            }
        }

        private void Opt1_Checked(object sender, RoutedEventArgs e)
        {

            answerid = Opt1.Tag.ToString();
        }

        private void Opt2_Checked(object sender, RoutedEventArgs e)
        {
            answerid = Opt2.Tag.ToString();
        }

        private void Opt3_Checked(object sender, RoutedEventArgs e)
        {
            answerid = Opt3.Tag.ToString();
        }

        private void Opt4_Checked(object sender, RoutedEventArgs e)
        {
            answerid = Opt4.Tag.ToString();
        }

        private void btnVote_Click(object sender, RoutedEventArgs e)
        {

            if (Opt1a.IsChecked == false && Opt2a.IsChecked == false && Opt3a.IsChecked == false && Opt4a.IsChecked == false)
            {
                MessageBox.Show("Merci de sélectionner une réponse avant soumttre de votre choix");
            }
            else
            {
                postforum();
            }
        }

        private void btnResult_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PollResults.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}